/*
 * Created on 2005-4-19
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.hezuti.common.page;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;




import com.hezuti.common.action.BaseAction;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 * @author xiaoyu.dou
 * 
 */
@SuppressWarnings("serial")
public class PageTag extends ComponentTagSupport {

	private static final String REQUSET_ENCODING = "UTF-8";

	private String action;

	private String vm = "vm/page/default.vm";

	/**
	 * 
	 */
	@Override
	public Component getBean(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
		if (action == null) {
			action = (String) stack.findValue(BaseAction.CURRENT_URI_NAME, String.class);
		}
		Set<String> excluedsSet = getExcluedsSet();
		String otherParams = getOtherParams(request, excluedsSet);
		ApplicationContext parent = (ApplicationContext) ServletActionContext.getServletContext().getAttribute(
				WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		VelocityEngine engine = (VelocityEngine) parent.getBean("velocityEngine");
		return new PageComponent(stack, vm, action, otherParams, engine);
	}

	@SuppressWarnings("rawtypes")
	private String getOtherParams(HttpServletRequest request, Set<String> excluedsSet) {
		StringBuffer sb = new StringBuffer();
		for (Enumeration enums = request.getParameterNames(); enums.hasMoreElements();) {
			String key = (String) enums.nextElement();
			String[] values = request.getParameterValues(key);
			if (isExcludeParam(excluedsSet, key)) {
				continue;
			}
			if (values == null || values.length == 0) {
				continue;
			}
			for (int i = 0; values != null && i < values.length; i++) {
				if (StringUtils.isEmpty(values[i]))
					continue;
				try {
					sb.append("&").append(key).append("=").append(URLEncoder.encode(values[i], REQUSET_ENCODING));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
		return sb.length() > 0 ? sb.substring(1) : sb.toString();
	}

	/**
	 * 判断是否要过滤的参数
	 * 
	 * @param excluedsSet
	 * @param param
	 * @return
	 */
	private boolean isExcludeParam(Set<String> excluedsSet, String param) {
		return excluedsSet == null ? false : excluedsSet.contains(param);
	}

	private Set<String> getExcluedsSet() {
		Set<String> set = new HashSet<String>();
		set.add(PageInfo.CURRENT_PAGE_NAME);
		return set;
	}

	/**
	 * 
	 * @return
	 */
	public String getAction() {
		return action;
	}

	/**
	 * 
	 * @param action
	 */
	public void setAction(String action) {
		this.action = action;
	}

	/**
	 * 
	 * @return
	 */
	public String getVm() {
		return vm;
	}

	/**
	 * 
	 * @param vm
	 */
	public void setVm(String vm) {
		this.vm = vm;
	}
}
